
<?php 

require ("../lib/upload.php");
require ("../lib/imaging.php");


if($_POST['addnew']==1){
	if($_FILES['file']['size']){
		$x=uploadimage_photo('file');
		if($x['msg']==''){
			$a=array();
			
			$a['name']		=clean_value($_POST['name']);
			$a['active']	=intval($_POST['active']);
			$a['thu_tu']	=intval($_POST['thu_tu']);
			$a['ngay_dang']	=time()+$CONFIG['time_offset'];
			$a['id_catpt']	= intval($_POST['parentid']);
			$a['id_user']	=intval($_SESSION["session_user_id"]);
			
			$a['image']=$x['image'];
			$a['normal_image']=$x['normal_image'];
			$a['small_image']=$x['small_image'];
			$b=$DB->compile_db_insert_string($a);
			$sql="INSERT INTO photo (".$b['FIELD_NAMES'].") VALUES (".$b['FIELD_VALUES'].")";
			$DB->query($sql);
			$id=mysql_insert_id();
			echo '<div class="div_show_photo_upload" id="photo_id_'.$id.'"><img  src="../'.$CONFIG['upload_image_path'].$x['small_image'].'" id="image" /><div class="div_tool_image"><a href="#" onclick="delete_image(\''.$id.'\'); return false;">&nbsp;Xóa ảnh</a></div></div>';
		}else{
			echo '<div class="div_show_photo_upload" id="photo_id_"><span style="color:red"><strong>Lỗi không upload được ảnh !</strong> </span><br>'.$x['msg'].'</div>';
		}
	}else{
		echo	'<div class="div_show_photo_upload" id="photo_id_">Bạn chưa chọn file upload !</div>';
	}
}
if($_POST['update']==1){
	$id=intval($_POST['id']);
	$sql="SELECT * FROM photo WHERE id_photo=".$id;
	$db=$DB->query($sql);
	if($rs=mysql_fetch_array($db)){
		$a=array();
		$a['name']			=clean_value($_POST['name']);
		$a['id_catpt']		=intval($_POST['parentid']);
		$a['thu_tu']		=intval($_POST['thu_tu']);
		$a['active']		=intval($_POST['active']);
		if($_FILES['file']['size']){
			deleteimage($rs['image'],$rs['normal_image'],$rs['small_image']);
			$x=uploadimage_photo('file');
			if($x['msg']==''){
				$a['image']=$x['image'];
				$a['small_image']=$x['small_image'];
				$a['normal_image']=$x['normal_image'];
			}
		}	
		$b=$DB->compile_db_update_string($a);
		$sql="UPDATE photo SET ".$b." WHERE id_photo=".$id;
		$DB->query($sql);
		echo '<div class="div_show_photo_upload" id="photo_id_'.$id.'"><img  src="../'.$CONFIG['upload_image_path'].$x['small_image'].'" id="image" height="160" width="160" /><div class="div_tool_image"></div>';
	}
}
if($_GET['delete_image']==1){
	$id=intval($_GET['id']);
	$sql="SELECT * FROM photo WHERE id_photo=".$id;
	$db=$DB->query($sql);
	if($rs=mysql_fetch_array($db)){
		deleteimage($rs['image'],$rs['normal_image'],$rs['small_image']);	
		
	}
	$DB->query("DELETE FROM photo WHERE id_photo=".$id);
}


function uploadimage_photo($inputname,$thumnail='yes',$dich=''){
	global $CONFIG,$_FILES,$tpl,$category,$par_page;
	$a=array();
	if($dich==''){
		$dich=$CONFIG['root_path'].$CONFIG['upload_image_path'];
	}else {
		$dich=$dich;
	}
	if($category==''){
		$category=$par_page;
	}
	if ($_FILES[$inputname]['size']){
		$in_image=get_new_file_name($_FILES[$inputname]['name'],clean_url(get_file_name($_FILES[$inputname]['name'])));
		$file_upload=new Upload($dich,'jpg,gif,png,bmp,swf');
		if ($file_upload->upload_file($inputname,2,$in_image))
		{
			//Da upload thanh cong
			//Tao thumbnail
			$a['image']=$file_upload->file_name;
			if($CONFIG['watermark_image']){
				$watermark = WideImage::load($CONFIG['root_path'].$CONFIG['upload_image_path'].$CONFIG['watermark_image']);
				$base = WideImage::load($dich.$a['image']);
				$result = $base->merge($watermark, "right - 10", "bottom - 10", 100);
			    $result->saveToFile($dich.$a['image']);
			}elseif($CONFIG['watermark_text']){
				 $img = WideImage::load($dich.$a['image']);
				 $canvas = $img->getCanvas();
				
				 $canvas->useFont('VARISON.TTF', 13, $img->allocateColor(255, 255, 255));
				 $canvas->writeText("right - 9", "top + 11", $CONFIG['watermark_text']);
				 $canvas->useFont('VARISON.TTF', 13, $img->allocateColor(9, 171, 25));
				 $canvas->writeText("right - 10", "top + 10", $CONFIG['watermark_text']);
				 $img->saveToFile($dich.$a['image']);	
				
			}
		  	deleteimage($dich.$a['image']);
			if($thumnail=='yes'){
				$thumbnail=create_thumb($dich, $file_upload->file_name);
				if ($thumbnail)
				{
					$a['small_image']=$thumbnail['thumb'];
					$a['normal_image']=$thumbnail['normal'];
				}
				else
				{
					$msg.="Kh&#244;ng t&#7841;o &#273;&#432;&#7907;c &#7843;nh thumbnail ! Xem l&#7841;i &#273;&#7883;nh d&#7841;ng file !<br>";
				}
			}
		}
		else
		{
			$msg.=$file_upload->get_upload_errors()."<br>";
		}			
		$a['msg']=$msg;
	}
	return $a;
}


?>



